package cn.edu.zjgsu.ito.controller;

import java.text.SimpleDateFormat;

import cn.edu.zjgsu.ito.interceptor.StudentControllerInterceptor;
import cn.edu.zjgsu.ito.service.ServiceFactory;
import cn.edu.zjgsu.ito.service.StuInfService;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;

@Before(StudentControllerInterceptor.class)
public class StuInfController extends Controller {
	/*
	 * 张虎涛
	 * 2016/06/01
	 */

	private StuInfService stuInfoService =ServiceFactory.createStuInfService();
	private String passportNo;
	private Record record;
	private boolean TF1;
	private boolean TF2;
	private boolean TF3;
	private String message;


	private String oldPassword;
	private String newPassword;
	private String rePassword;
	private String phone;
	private String mailbox;

	/**
	 * 学生用户查看自身信息
	 */
	public void index(){
		try{
			if(getSessionAttr("passportNo")==null){
				System.out.println("登录超时，请从新登陆！");	
			}else{
				passportNo=getSessionAttr("passportNo");
			}
//			record=stuInfoService.search(passportNo);
			Record studentInfo = stuInfoService.search(passportNo);
			if(studentInfo==null){
				redirect("/toErrors");
				return;
			}
			else{
				setAttr("studentInfo",studentInfo);
				render("sinformation.html");
			}	
		}
		catch(Exception e){

		}
	}

	/**
	 * 学生跳转修改页面
	 */
	public void toUpdateStu(){
		try{
			if(getSessionAttr("passportNo")==null){
				System.out.println("登录超时，请从新登陆！");	
			}else{
				passportNo=getSessionAttr("passportNo");
			}
			record=stuInfoService.search(passportNo);

			if(record==null){
				redirect("/toErrors");
				return;
			}
			else{
				setAttr("record",record);
				render("correctinfo.html");
			}	
		}
		catch(Exception e){

		}
	}

	/**
	 * 学生用户修改自身信息
	 */
	public void update(){
		TF1=false;//初始化
		TF2=false;//初始化
		TF3=false;//初始化
		message="";//初始化

		phone=getPara("phone");
		mailbox=getPara("mailbox");


		oldPassword=getPara("oldPassword");
		newPassword=getPara("newPassword");
		rePassword=getPara("rePassword");
		try{
			if(getSessionAttr("passportNo")==null){
				render("../common/errors/outlogin.html");	
			}
			else{
				passportNo=getSessionAttr("passportNo");
                
                if(!phone.equals("")){
                    boolean tf=stuInfoService.updateStuInfPhone(phone,passportNo);
                    message+="  电话修改成功  ";
                }
                if(!mailbox.equals("")){
                    boolean tf=false;
                    tf=stuInfoService.updateStuInfMail(mailbox,passportNo);
                    message+="  邮箱修改成功  ";                    
                }
				
			}
			if(oldPassword!=null && newPassword!=null && rePassword!=null){
				TF1=stuInfoService.checkPassword(passportNo, oldPassword);
				if(TF1){
					TF2=stuInfoService.updatePassword( newPassword,passportNo);
				}else{
					message="原密码错误";
				}
				if(!TF2){

					message="密码修改失败错误";
				}
			}else{
                if((oldPassword==null||oldPassword.equals("")) ){
                    message+=" 原始密码不能为空 ";
					setAttr("message","原始密码不能为空");
					toUpdateStu();
					return;
				}
				if(newPassword==null||newPassword.equals("")||rePassword==null||rePassword.equals("")){
					message+=" 新密码不能为空 ";
					setAttr("message","新密码不能为空");
					toUpdateStu();
					return;
				}

				if(!newPassword.equals(rePassword)){
                    message+=" 两次密码不一致 ";
					setAttr("message","两次密码不一致");
					toUpdateStu();
					return;
				}
            }
			if(TF2){
				redirect("/upPass");
				return;
			}else{
                
				index();
				return;
			}

		}
		catch(Exception e){

		}

	}

}
